/**   
 * @Title: HuoDcontent.java 
 * @Package com.esun.cheetah.view.news
 * @Description: (活动详情) 
 * @author 孔祥攀 <br/>
 * @date 2012-6-19 上午10:29:10
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.view.news;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.news.NewsContentCommunicater;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.model.news.NewsHuoDContentBean;
import com.esun.cheetah.network.ConnectionChangeReceiver;
import com.esun.cheetah.network.NetImageView.NetImageView;
import com.esun.cheetah.view.personal.sportlotteryfootball.LoginBetAct;
import com.esun.cheetah.view.setting.UserSuggestAct;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * 
 * @Package com.esun.cheetah.view.news
 * @ClassName: HuoDcontent
 * @Description: TODO(活动详情)
 * @author 孔祥攀 <br/>
 * @date 2012-6-19 上午10:29:10
 * 
 */
public class HuoDContent extends Activity implements OnClickListener {

  private Button btnBack;
  private Button btnFanK;
  private TextView tvHuoDHead;
  private TextView tvHuoDsHead;
  private TextView tvHuoDsucHead;

  private TextView tvHuoDNote;
  private Button btnZhongJ;

  private TextView tvHuoDTime;
  private TextView tvHuoDContent;
  // private TextView tvHuoDContent1;
  private TextView tvHuoDRule;

  private NewsContentCommunicater newsContentCommunicater;

  private NewsHuoDContentBean huodbean;

  private Handler handlerHuoDContent;
  private ProgressDialog progressDialog;

  private String serverMessage;

  private  String id;
  // private String username="";
  private String status;

  private Bundle bundle;
  
  
  
  
  
  private ProgressBar progressBar;
  private Bitmap bitmap = null;
//  private ProgressDialog dialog = null;

  
  
  private String PATH;

  private NetImageView mNetImageView;
  private EditText picaddress;
  private Button button;
  private ImageView imageView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_huod_content);
    initHandler();
    progressBar = (ProgressBar) findViewById(R.id.progressbar);
    progressBar.setVisibility(View.GONE);
  
    PATH = "http://ws.500wan.com/pages/interface/" + "client/webview/images/activity/" + "9693" + ".png";
    
//    dialog = ProgressDialog.show(HuoDContent.this, "",
//        "正在下载，请稍等···");
    new Thread() {
      @Override
      public void run() {
        try {
          // 让ProgressDialog显示一会儿。。。。
          Thread.sleep(2000);
          URL url = new URL(PATH);
          // 建立网络连接
          HttpURLConnection conn = (HttpURLConnection) url
              .openConnection();
          InputStream inputStream = conn.getInputStream();
          bitmap = BitmapFactory.decodeStream(inputStream);
          inputStream.close();
          Message message = new Message();
          message.what = 1;
          handlerHuoDContent.sendMessage(message);
        } catch (Exception e) {
          Message message = new Message();
          message.what = -1;
          handlerHuoDContent.sendMessage(message);
          e.printStackTrace();
        }
      }
    }.start();

    initUI();
    imageView=(ImageView)this.findViewById(R.id.image);
 
  }
  


  // 初始化
  public void initUI() {

    progressDialog = ViewerUtil.showProgressDialog(HuoDContent.this, getResources().getString(R.string.loading_text));
    progressDialog.show();
    bundle = this.getIntent().getExtras();
    status = bundle.getString("status");
    id = bundle.getString("id");

    btnZhongJ = (Button) findViewById(R.id.news_huod_content_btn_zhongjiang);
    btnBack = (Button) findViewById(R.id.news_huod_content_btn_goBack);
    btnFanK = (Button) findViewById(R.id.news_huod_content_btn_fank);
    btnBack.setOnClickListener(this);
    btnFanK.setOnClickListener(this);
    btnZhongJ.setOnClickListener(this);
    tvHuoDHead = (TextView) findViewById(R.id.news_huod_content_head);
    tvHuoDsucHead = (TextView) findViewById(R.id.news_huod_content_suchead);
    tvHuoDsHead = (TextView) findViewById(R.id.news_huod_content_shead);
    tvHuoDTime = (TextView) findViewById(R.id.news_huod_content_time);
    tvHuoDContent = (TextView) findViewById(R.id.news_huod_content_content);

    tvHuoDNote = (TextView) findViewById(R.id.news_huod_content_note);
    // tvHuoDContent1 = (TextView)
    // findViewById(R.id.news_huod_content_content1);
    tvHuoDRule = (TextView) findViewById(R.id.news_huod_content_rule);

    if (status.equals("2")) {
      btnFanK.setText("反馈请点击");
    } else {
      btnFanK.setText("我中奖了么?");
    }

    // getNewsContentThread();
  }

  // 获取详细信息线程
  public void getNewsContentThread() {
    newsContentCommunicater = (NewsContentCommunicater) new NewsContentCommunicater(handlerHuoDContent, id);
    newsContentCommunicater.startCommunicate();

  }

  // 通讯结果处理
  public void initHandler() {
    handlerHuoDContent = new Handler() {

      @Override
      public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        switch (msg.what) {
          case Config.COMMUNICATER_SUCCESS:
            // huodbean = newsContentCommunicater.get;
            communicaterSucceed();

            break;
          case Config.COMMUNICATER_FAIL:

            // ViewerUtil.showTipDialog(
            // HuoDContent.this, serverMessage,
            // false);

            break;
          case Config.COMMUNICATER_EXCEPTION:

            // ViewerUtil.showTipDialog(
            // HuoDContent.this,
            // Language.STR_CONNECTIONFAIL, false);
            Toast.makeText(HuoDContent.this, "获取失败", 0).show();

            break;
          case 1:
            imageView = (ImageView) findViewById(R.id.image);
//            dialog.dismiss();
            imageView.setImageBitmap(bitmap);
            break;
          case -1:
            // 下载失败使用默认图片
            imageView = (ImageView) findViewById(R.id.image);
//            dialog.dismiss();
            imageView.setBackgroundResource(R.drawable.icon);
            break;
            

          default:
            break;
        }
        progressDialog.dismiss();
      }
    };
  }

  // 详情显示
  public void communicaterSucceed() {

    if (newsContentCommunicater.getActivityHeadNote().equals("")) {
      EDebug.o("newsContentCommunicater.getActivityHeadNote()----2--" + newsContentCommunicater.getActivityHeadNote());
      tvHuoDsucHead.setVisibility(View.GONE);
    } else {
      tvHuoDsucHead.setText(newsContentCommunicater.getActivityHeadNote().replace("<br />\r", ""));
      // tvHuoDsucHead.setVisibility(View.VISIBLE);
    }

    tvHuoDHead.setText(newsContentCommunicater.getBigtitle());
    EDebug.o("newsContentCommunicater.getBigtitle()123------" + newsContentCommunicater.getBigtitle());

    tvHuoDsHead.setText(newsContentCommunicater.getNote());

    tvHuoDTime.setText(newsContentCommunicater.getActivityTime());

    // String s = newsContentCommunicater.getContent().replace("<br />\r","") ;

    tvHuoDContent.setText(newsContentCommunicater.getContent().replace("<br />\r", ""));
    // EDebug.o("newsContentCommunicater.getBigtitle()123------" +
    // newsContentCommunicater.getContent().replace("<br />",""));
    // tvHuoDContent1.setText(newsContentCommunicater.getContent1());

    tvHuoDRule.setText(newsContentCommunicater.getActivityRule());

    tvHuoDNote.setText(newsContentCommunicater.getActivityNotes());

  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
      case R.id.news_huod_content_btn_goBack:

        HuoDContent.this.finish();

        break;
      case R.id.news_huod_content_btn_zhongjiang:

        if (status.equals("2")) {
          Toast.makeText(HuoDContent.this, "活动进行中，请结束后查看", 0).show();

        } else {

          Intent intent1 = new Intent(HuoDContent.this, NewsHuoDZHJ.class);
          startActivity(intent1);

        }

        break;

      case R.id.news_huod_content_btn_fank:
        if (status.equals("2")) {

          EDebug.o("dafsdfasdf");
          if (UserInfo.getUserInfo().getCk().length() < 3) {
            Intent intent = new Intent();

            intent.setClass(HuoDContent.this, LoginBetAct.class);
            intent.putExtra("flag", "0");
            startActivity(intent);
          } else {
            Intent intent2 = new Intent(HuoDContent.this, UserSuggestAct.class);
            startActivity(intent2);
          }

        } else {

          if (UserInfo.getUserInfo().getCk().length() < 3) {
            Intent intent = new Intent();

            intent.setClass(HuoDContent.this, LoginBetAct.class);
            intent.putExtra("flag", "2");
            startActivity(intent);
            tvHuoDsucHead.setVisibility(View.VISIBLE);
          } else {
            tvHuoDsucHead.setVisibility(View.VISIBLE);
          }

        }

        break;

      default:
        break;
    }
  }

  @Override
  protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    // isShow = false;
    this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
  }

  // 将menu键点击事件返回给父activity处理
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (keyCode == KeyEvent.KEYCODE_MENU) {
      Activity parent = getParent();
      if (parent != null) {
        parent.onKeyDown(keyCode, event);
        return true;
      }
    }
    return super.onKeyDown(keyCode, event);
  }

  @Override
  protected void onResume() {
    getNewsContentThread();
    // TODO Auto-generated method stub
    super.onResume();
    // isShow = true;
    this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(),
        ConnectionChangeReceiver.getIntentFilter());
  }
  // AsyncTask异步方式下载图片
  class DownImageTask extends AsyncTask<String, Integer, Bitmap> {
    // 执行预处理
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      // 显示进度条
      progressBar.setVisibility(View.VISIBLE);
      progressBar.setMax(100);
    }

    // 后台进程的执行
    @Override
    protected Bitmap doInBackground(String... params) {
      try {
        URL url = new URL(params[0]);
        HttpURLConnection conn = (HttpURLConnection) url
            .openConnection();
        InputStream inputStream = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
        // 进度条的更新，我这边只是用一个循环来示范，在实际应用中要使用已下载文件的大小和文件总大小的比例来更新
        for (int i = 1; i <= 10; i++) {
          publishProgress(i * 10);
          Thread.sleep(200);
        }
        inputStream.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
      return bitmap;
    }

    // 运行于UI线程，对后台任务的结果做出处理，doInBackground方法执行的结果作为此方法的参数
    @Override
    protected void onPostExecute(Bitmap result) {
      super.onPostExecute(result);
      ImageView imageView = (ImageView) findViewById(R.id.image);
      imageView.setImageBitmap(result);
      progressBar.setVisibility(View.GONE);
    }

    // 运行于UI线程，如果在doInBackground(Params...)中使用了publishProgress(Progress...)，就会触发此方法
    @Override
    protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      progressBar.setProgress(values[0]);
    }
  }
}